﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Data;
using Runda.CMS.Common.Utility;
using Telerik.Web.UI;

namespace Runda.CMS.Portal.Budget
{
    public partial class RptSalesBudgetBySalesGroup : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "customername";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "DepartmentCode";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                dt = new BudgetBll().Departmentlist(base.CurrentUserID.ToString());
                ddlDepartment.DataSource = dt;
                ddlDepartment.DataTextField = "Departmentname";
                ddlDepartment.DataValueField = "Departmentname";
                ddlDepartment.DataBind();
                ddlDepartment.Items.Add(new ListItem("--不限--", "0"));
                ddlDepartment.SelectedValue = "0";

                dt = new BudgetBll().Saleslist(base.CurrentUserID.ToString());
                ddlSales.DataSource = dt;
                ddlSales.DataTextField = "username";
                ddlSales.DataValueField = "username";
                ddlSales.DataBind();
                ddlSales.Items.Add(new ListItem("--不限--", "0"));
                ddlSales.SelectedValue = "0";

                dt = new BudgetBll().ProductCategorylist();
                ddlProductCategory.DataSource = dt;
                ddlProductCategory.DataTextField = "categoryName";
                ddlProductCategory.DataValueField = "categoryName";
                ddlProductCategory.DataBind();
                ddlProductCategory.Items.Add(new ListItem("--不限--", "0"));
                ddlProductCategory.SelectedValue = "0";
               
                BindGrid();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            //if (e.CommandName.ToLower() == "delete")
            //{
            //    BudgetBll bll = new BudgetBll();
            //    bll.DeleteByBudgetID(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["BudgetID"].ToString()));
            //}
            //BindGrid(AspNetPager1.CurrentPageIndex);
        }

        
        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
        } 

        private void BindGrid()
        {
            string sb = getWhereClause();

            DataTable dt = new BudgetBll().GenReportData(2, sb);

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        private string getWhereClause()
        { 
            StringBuilder sb = new StringBuilder();

            //if (base.CurrentUserEntity.UserName == "郭海涛"
            //    || base.CurrentUserEntity.UserName == "史莹"
            //    || base.CurrentUserEntity.UserName == "王静"
            //    || base.CurrentUserEntity.UserName == "顾悦"
            //    || base.CurrentUserEntity.UserName == "宋欣"
            //    || base.CurrentUserEntity.UserName == "徐雪峰"
            //    || base.CurrentUserEntity.UserName == "胡日成"
            //    || base.CurrentUserEntity.UserName == "崔丽丽"
            //    || base.CurrentUserEntity.UserName == "官俊"
            //    || base.CurrentUserEntity.UserName == "李娜"
            //    || base.CurrentUserEntity.UserName == "张佳锦"
            //    )
            //{
            //    sb.Append(" and (s.[company] in(").Append("select DepartmentCode from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("')))");
            //}
            //else
            //{
            //    sb.Append(" and ([sales] in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            //    sb.Append(" or [sales] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            //    sb.Append(" or [Producter] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            //    sb.Append(" or [ProductManager] ='").Append(base.CurrentUserEntity.UserName).Append("')");
            //}

            sb.Append(" and ((s.company in(").Append("select DepartmentCode from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            sb.Append(" and left(productCategoryCode,6) in(").Append("select categoryCode from Cms_Ctrt_InstrumentCategory where categoryGUID in(select categoryGUID from Cms_sys_UserProductCategory where UserID ='").Append(base.CurrentUserID.ToString()).Append("')))");
            sb.Append(" or [sales] in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            sb.Append(" or [sales] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            sb.Append(" or [Producter] ='").Append(base.CurrentUserEntity.UserName).Append("')");

            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and [BudgetYear]  = '").Append(ddlBudgetYear.SelectedValue).Append("'");
            }

            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and company='").Append(ddlCompany.SelectedValue.ToString()).Append("'");
            }

            if (this.ddlDepartment.SelectedValue != "0")
            {
                sb.Append(" and sales in(select UserName from vw_sysUser where DepartmentName='").Append(ddlDepartment.SelectedValue.ToString()).Append("')");
            }

            if (ddlSales.SelectedValue != "0")
            {
                sb.Append(" and sales='").Append(ddlSales.SelectedValue.ToString()).Append("'");
            }

            if (this.ddlProductCategory.SelectedValue != "0")
            {
                sb.Append(" and productCategoryCode in(select CategoryCode from Cms_Ctrt_InstrumentCategory where FactoryName='").Append(ddlProductCategory.SelectedValue.ToString()).Append("')");
            }

            if (txtCustomerName.Text.Trim() != "")
            {
                sb.Append(" and customerID in(").Append("select customerid from cms_ctrt_customer where customername like '%").Append(txtCustomerName.Text.Trim()).Append("%')");
            }

            return sb.ToString();
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string sb = getWhereClause();

            DataTable dt = new BudgetBll().GenReportData(2, sb);

            Export.Export2Excel(dt, "销售预算一览表(按客户）", Page.Response);

        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                if (dv != null)
                {
                    e.Item.Attributes.Add("alerttype ", dv["RowType"].ToString());
                   
                }
               
            }

            


        }

        protected void gridAccessoryList_PreRender(object sender, EventArgs e)
        {
            //for (int rowIndex = gridAccessoryList.Items.Count - 2; rowIndex >= 0; rowIndex--)
            //{
            //    TableCell tc1 = gridAccessoryList.MasterTableView.Items[rowIndex];

            //    GridDataItem row = gridAccessoryList.Items[rowIndex];
            //    string sales = row["销售小组"].Text;
            //    GridDataItem previousRow = gridAccessoryList.Items[rowIndex + 1];
            //    if (row["销售小组"].Text == previousRow["销售小组"].Text)
            //    {
            //        row["销售小组"].RowSpan = previousRow["ColumnUniqueName"].RowSpan < 2 ? 2 : previousRow["销售小组"].RowSpan + 1;
            //        previousRow["销售小组"].Visible = false;
            //    }
            //}

            for (int i = 0; i < gridAccessoryList.MasterTableView.Items.Count; i++)
            {
                if (i < 1) continue;
                bool flag1 = (gridAccessoryList.MasterTableView.Items[i].ItemType == Telerik.WebControls.GridItemType.AlternatingItem || gridAccessoryList.MasterTableView.Items[i].ItemType == Telerik.WebControls.GridItemType.Item);
                bool flag2 = (gridAccessoryList.MasterTableView.Items[i - 1].ItemType == Telerik.WebControls.GridItemType.AlternatingItem || gridAccessoryList.MasterTableView.Items[i - 1].ItemType == Telerik.WebControls.GridItemType.Item);
                if (flag1 && flag2)
                {
                    TableCell tc1 = gridAccessoryList.MasterTableView.Items[i]["销售小组"];
                    TableCell tc2 = gridAccessoryList.MasterTableView.Items[i - 1]["销售小组"];
                    if (tc1.Text == tc2.Text)
                    {
                        int k = 1;
                        while (!tc2.Visible)
                        {
                            k++;
                            tc2 = gridAccessoryList.MasterTableView.Items[i - k]["销售小组"];
                        }
                        tc2.RowSpan = k + 1;
                        tc2.VerticalAlign = VerticalAlign.Middle;

                        tc1.Visible = false;
                    }
                }
            }

        } 

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {

            DataTable dt = new BudgetBll().Departmentlist(base.CurrentUserID.ToString(), ddlCompany.SelectedValue.ToString());
            ddlDepartment.DataSource = dt;
            ddlDepartment.DataTextField = "Departmentname";
            ddlDepartment.DataValueField = "Departmentname";
            ddlDepartment.DataBind();
            ddlDepartment.Items.Add(new ListItem("--不限--", "0"));
            ddlDepartment.SelectedValue = "0";
        }
    }
}
